<?php

namespace api\modules\v1\controllers\rbac;

use Yii;
use api\controllers\OnAuthController;
use api\modules\v1\forms\rbac\DepartmentForm;
use common\helpers\CommonHelper;
use common\models\rbac\Department;

/**
 * Class DepartmentController
 * @package api\modules\v1\controllers\rbac
 */
class DepartmentController extends OnAuthController
{
    public $modelClass = Department::class;


    /**
     * @api {get} /v1/rbac/department/list 部门列表
     * @apiName list
     * @apiGroup Department
     *
     * @apiParam {Number} page                          当前页码
     * @apiParam {Number} pageSize                      每页显示数
     *
     * @apiSuccess {Object} data                        返回数据
     * @apiSuccess {Number} data.page                   当前页码
     * @apiSuccess {Number} data.pageSize               每页显示数
     * @apiSuccess {Number} data.total                  总数
     * @apiSuccess {Array}  data.list                   部门列表
     * @apiSuccess {Number} data.list.id                部门ID
     * @apiSuccess {String} data.list.name              部门名称
     * @apiSuccess {String} data.list.department_leader 负责人
     * @apiSuccess {Number} data.list.status            部门状态
     * @apiSuccess {String} data.list.status_str        部门状态值
     * @apiSuccess {Number} data.list.total_num         部门总人数
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $data = Yii::$app->services->department->departmentListPage($params, (int)$page, (int)$pageSize);

        // 格式化列表数据
        $data['list'] = Yii::$app->services->department->departmentListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {post} /v1/rbac/department/info 部门详情
     * @apiName info
     * @apiGroup Department
     *
     * @apiParam {Number} id                                            部门ID
     *
     * @apiSuccess {Object} data                                        返回数据
     * @apiSuccess {Number} data.id                                     部门ID
     * @apiSuccess {Number} data.name                                   部门名称
     * @apiSuccess {Number} data.status                                 部门状态
     * @apiSuccess {String} data.department_leader                      部门负责人姓名
     * @apiSuccess {String} data.department_leader_ids                  部门负责人IDs
     * @apiSuccess {Array}  data.member_list                            成员列表
     * @apiSuccess {Number} data.member_list.id                         成员UID
     * @apiSuccess {String} data.member_list.realname                   成员姓名
     * @apiSuccess {Number} data.member_list.is_department_leader       是否为负责人【1是、0否】
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id', 0);

        // 部门信息
        $department = Yii::$app->services->department->findById($id);

        // 用户列表
        $memberList = Yii::$app->services->member->findListInnerAttr([
            'department_id' => [0, $id],
            'project_id' => $this->projectId,
        ]);

        // 格式化用户列表
        $memberListFormat = Yii::$app->services->member->findListInnerAttrFormat($memberList);

        return [
            'id' => $department->id ?? 0,
            'name' => $department->name ?? '',
            'status' => $department->status ?? Department::STATUS_ENABLE,
            'department_leader' => $department->department_leader ?? '',
            'department_leader_ids' => $memberListFormat['leader_ids'],
            'member_list' => $memberListFormat['list'],
        ];
    }

    /**
     * @api {post} /v1/rbac/department/create 新增部门
     * @apiName create
     * @apiGroup Department
     *
     * @apiParam {String} name          部门名称
     * @apiParam {Number} status        部门状态【1正常、0停用】
     * @apiParam {String} leader_ids    部门负责人IDs【多个用','分割】
     *
     * @apiSuccess {Object} data        返回数据
     * @apiSuccess {Number} data.id     部门ID
     */
    public function actionCreateDepartment()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        $params['member_id'] = Yii::$app->user->identity->member_id;

        $model = new DepartmentForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        // 其他参数
        $otherParams = [
            'member_id' => Yii::$app->user->identity->member_id,
        ];
        return Yii::$app->services->department->create($model, $otherParams);
    }

    /**
     * @api {post} /v1/rbac/department/update 编辑部门
     * @apiName update
     * @apiGroup Department
     *
     * @apiParam {Number} id            部门ID
     * @apiParam {String} name          部门名称
     * @apiParam {Number} status        部门状态【1正常、0停用】
     * @apiParam {String} leader_ids    部门负责人IDs【多个用','分割】
     *
     * @apiSuccess {Object} data        返回数据
     * @apiSuccess {Number} data.id     部门ID
     */
    public function actionUpdateDepartment()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        $params['member_id'] = Yii::$app->user->identity->member_id;


        $model = new DepartmentForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        // 其他参数
        $otherParams = [
            'member_id' => Yii::$app->user->identity->member_id,
        ];
        return Yii::$app->services->department->edit($model, $otherParams);
    }

    /**
     * @api {post} /v1/rbac/department/update-status 编辑部门状态
     * @apiName update-status
     * @apiGroup Department
     *
     * @apiParam {Number} id            部门ID
     * @apiParam {Number} status        部门状态【1正常、0停用】
     *
     * @apiSuccess {Object} data        返回数据
     * @apiSuccess {Number} data.id     部门ID
     */
    public function actionUpdateDepartmentStatus()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        $params['member_id'] = Yii::$app->user->identity->member_id;


        $model = new DepartmentForm();
        $model->setScenario('edit_status');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->department->editStaus($model);
    }

}